package com.example.emergencyattempt01.activity.ex;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.utils.AutoPager_Adapter;
import com.example.emergencyattempt01.activity.utils.FixedSpeedScroller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class AutoPagerBuilder {
    private AutoPager autoPager;
    private AppCompatActivity parent;// 从外部传入，表示本构造对象的父活动
    private final int MOVE_TO_NEXT = 0x001;


    public AutoPagerBuilder(AppCompatActivity parent) {
        this.parent = parent;
    }
   // 使用前务必先初始化
    private void initPager()
    {
        autoPager=new AutoPager();
        autoPager.viewPager=parent.findViewById(R.id.auto_slide_view);
    }

    private AutoPager setBitmaps()// 不提供参数，直接写死，因为图片固定就那5张
    {
        List<Bitmap> bitmaps=new ArrayList<>();
        bitmaps.add(BitmapFactory.decodeResource(parent.getResources(),R.drawable.lib1));
        bitmaps.add(BitmapFactory.decodeResource(parent.getResources(),R.drawable.lib2));
        bitmaps.add(BitmapFactory.decodeResource(parent.getResources(),R.drawable.lib3));
        bitmaps.add(BitmapFactory.decodeResource(parent.getResources(),R.drawable.lib4));
        bitmaps.add(BitmapFactory.decodeResource(parent.getResources(),R.drawable.lib5));
        autoPager.setPagerBitmaps(bitmaps);
        return autoPager;
    }

    // 设置小圆点
    private AutoPager setDots()
    {
        ImageView[] dots=new ImageView[5];
        // 初始化
        dots[0]=parent.findViewById(R.id.dot0);
        dots[1]=parent.findViewById(R.id.dot1);
        dots[2]=parent.findViewById(R.id.dot2);
        dots[3]=parent.findViewById(R.id.dot3);
        dots[4]=parent.findViewById(R.id.dot4);
        // 先定义初态
        dots[0].setImageResource(R.drawable.shape_little_dot);
        dots[1].setImageResource(R.drawable.shape_little_dot_un);
        dots[2].setImageResource(R.drawable.shape_little_dot_un);
        dots[3].setImageResource(R.drawable.shape_little_dot_un);
        dots[4].setImageResource(R.drawable.shape_little_dot_un);
        autoPager.setDots(dots);
        return autoPager;
    }

    // 设置播放优化,要在实现自动切换之前使用
    private void setAutoPlayBetter()
    {
        try {
            Interpolator sInterpolator = new AccelerateInterpolator();
            Field mScroller;
            mScroller= ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true);
            FixedSpeedScroller scroller=new FixedSpeedScroller(parent,sInterpolator);
            scroller.setDuration(5000);//设置滑动的持续时间
            mScroller.set(parent,scroller);
        }catch (NoSuchFieldException e){}
        catch (IllegalArgumentException e){}
        catch (IllegalAccessException e){};
    }

    private void setAdapterForAuto()
    {
        autoPager.viewPager.setAdapter(new AutoPager_Adapter(parent,autoPager.pagerBitmaps));
    }
    // 先设置handler，再设置播放线程
    private void setHandler()
    {
        autoPager.setHandlerForAutoAdapter(new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == MOVE_TO_NEXT) {
                    //设置图片自动滑动
                    int position = ((Integer) (msg.obj)).intValue();
                    autoPager.viewPager.setCurrentItem(position, true);
                    //设置小圆点变化
                    for (int i = 0; i < 5; i++) {
                        if (position == i) {
                            autoPager.dots[i].setImageResource(R.drawable.shape_little_dot);
                        } else {
                            autoPager.dots[i].setImageResource(R.drawable.shape_little_dot_un);
                        }
                    }
                }
            }
        });
    }
   // 设置额外监听,此过程是为了优化，方便小圆点能够跟得上图片变化
    private void setdotsFollowing()
    {
        autoPager.viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()//此步骤是为了让小圆点能够跟上手指滑动的变化
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                for(int i=0;i<5;i++)
                {
                    if(position==i)
                    {
                        autoPager.dots[i].setImageResource(R.drawable.shape_little_dot);
                    }
                    else
                    {
                        autoPager.dots[i].setImageResource(R.drawable.shape_little_dot_un);
                    }
                }
            }
            @Override
            public void onPageSelected(int position) {

            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



    public AutoPager build()
    {
       initPager();// 初始化
       setBitmaps();// 设置图库
       setDots();// 设置小圆点
       setAutoPlayBetter();// 设置优化
       setAdapterForAuto();// 设置适配器
        setdotsFollowing();// 设置圆点跟随
       setHandler();// 设置handler
       return autoPager;
    }
}
